package com.demo.quartz.config;

import lombok.SneakyThrows;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

/**
 * 定时配置（可以配置静态定时任务）
 */
@Configuration
public class SchedulerConfig {

    @Autowired
    private SchedulerListener scheduleListener;

    // 是否开启监听器
    @Value("${jobListener.switch:false}")
    private boolean jobListener;


    @Bean
    public SchedulerFactory schedulerFactoryBean(){
        return new StdSchedulerFactory();
    }

    @SneakyThrows
    @Bean
    public Scheduler scheduler(@Qualifier("schedulerFactoryBean") SchedulerFactory schedulerFactoryBean){
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        if(jobListener){
            scheduler.getListenerManager().addJobListener(scheduleListener);
        }
        return scheduler;
    }
}
